package com.ruoyi.ucar.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ucar.boreq.ProductDetailQueryBoReq;
import com.ruoyi.ucar.boreq.ProductListQueryBoReq;
import com.ruoyi.ucar.boresp.ProductDetailQueryBoResp;
import com.ruoyi.ucar.boresp.ProductListQueryBoResp;
import com.ruoyi.ucar.boresp.ProductMobileListBoResp;
import com.ruoyi.ucar.common.CommonVoResp;
import com.ruoyi.ucar.service.ITProductService;
import com.ruoyi.ucar.domain.TProduct;
import com.ruoyi.ucar.util.CommonUtil;
import com.ruoyi.ucar.util.ResponseUtil;
import com.ruoyi.ucar.voreq.*;
import com.ruoyi.ucar.voresp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 产品Controller
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Api(value = "产品管理",tags = "产品管理模块接口")
@RestController
@RequestMapping("/ucar/product")
public class TProductController extends BaseController
{
    @Autowired
    private ITProductService tProductService;

    /**
     * 查询产品列表
     */
    @ApiOperation("查询产品列表-Mobile使用-排序规则：sortNumber升序")
    @Anonymous
    @PostMapping("/mobileList")
    public CommonVoResp<ProductMobileListVoResp> mobileList()
    {
        CommonVoResp<ProductMobileListVoResp> commonVoResp=new CommonVoResp<>();
        ProductMobileListBoResp productMobileListBoResp=tProductService.mobileList();
        if(StringUtils.isNotNull(productMobileListBoResp)){
            ProductMobileListVoResp productMobileListVoResp=new ProductMobileListVoResp();
            List<ProductMobileListVo> productMobileListVoList=new ArrayList<>();
            productMobileListBoResp.getProductMobileListBos().forEach(productMobileListBo -> {
                ProductMobileListVo productMobileListVo=CommonUtil.objToObj(productMobileListBo,ProductMobileListVo.class);
                productMobileListVoList.add(productMobileListVo);
            });
            productMobileListVoResp.setProductMobileListVos(productMobileListVoList);
            commonVoResp.setData(productMobileListVoResp);
        }else{
            return CommonUtil.setCommonVoRespNoDataError(commonVoResp);
        }
         logger.info("查询产品列表-Mobile使用出参：{}",commonVoResp);
         return commonVoResp;
    }

    /**
     * 查询产品列表
     */
    @ApiOperation("查询产品列表")
    @PostMapping("/list")
    public CommonVoResp<ProductListQueryVoResp> list(@RequestBody ProductListQueryVoReq  productListQueryVoReq)
    {
        logger.info("产品列表查询VO端入口参数:{}",productListQueryVoReq);
        CommonVoResp<ProductListQueryVoResp> commonVoResp=new CommonVoResp<>();
        if(
                productListQueryVoReq.getPageIndex()==null||
                        productListQueryVoReq.getPageSize()==null){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        ProductListQueryBoReq productListQueryBoReq=CommonUtil.objToObj(productListQueryVoReq,ProductListQueryBoReq.class);
        productListQueryBoReq.setPageIndex(productListQueryVoReq.getPageIndex());
        productListQueryBoReq.setPageSize(productListQueryVoReq.getPageSize());
        ProductListQueryBoResp productListQueryBoResp=tProductService.queryProductByCond(productListQueryBoReq);
        if(StringUtils.isNotNull(productListQueryBoResp)){
            ProductListQueryVoResp productListQueryVoResp=new ProductListQueryVoResp();
            List<ProductListQueryVo> productListQueryVos=new ArrayList<>();
            productListQueryVoResp.setTotalCount(productListQueryBoResp.getTotalCount());
            productListQueryVoResp.setTotalPage(productListQueryBoResp.getTotalPage());
            productListQueryBoResp.getProductListQueryBos().forEach(productListQueryBo -> {
                ProductListQueryVo productListQueryVo=CommonUtil.objToObj(productListQueryBo,ProductListQueryVo.class);
                productListQueryVos.add(productListQueryVo);
            });
            productListQueryVoResp.setProductListQueryVos(productListQueryVos);
            commonVoResp.setData(productListQueryVoResp);
        }else{
            return CommonUtil.setCommonVoRespNoDataError(commonVoResp);
        }
        logger.info("产品列表查询出参：{}",commonVoResp);
        return commonVoResp;
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('ucar:product:export')")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProduct tProduct)
    {
        List<TProduct> list = tProductService.selectTProductList(tProduct);
        ExcelUtil<TProduct> util = new ExcelUtil<TProduct>(TProduct.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @ApiOperation("获取产品详细信息接口-id和productUuid，两者传一即可")
    @PostMapping(value = "/detail")
    public CommonVoResp<ProductDetailQueryVoResp> productDetailQuery(@RequestBody ProductDetailQueryVoReq productDetailQueryVoReq)
    {
        CommonVoResp<ProductDetailQueryVoResp> commonVoResp=new CommonVoResp<>();
        logger.info("获取产品详细信息入参：{}",productDetailQueryVoReq);
        if(
                productDetailQueryVoReq.getProductUuid()==null&&
                        productDetailQueryVoReq.getId()==null){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        ProductDetailQueryBoResp productDetailQueryBoResp=tProductService.productDetailQuery(CommonUtil.objToObj(productDetailQueryVoReq, ProductDetailQueryBoReq.class));

        if(StringUtils.isNull(productDetailQueryBoResp)){
            return CommonUtil.setCommonVoRespNoDataError(commonVoResp);
        }else{
            ProductDetailQueryVoResp productDetailQueryVoResp=CommonUtil.objToObj(productDetailQueryBoResp,ProductDetailQueryVoResp.class);
            commonVoResp.setData(productDetailQueryVoResp);
        }
        logger.info("获取产品详细信息出参：{}",commonVoResp);
        return commonVoResp;
    }

    /**
     * 新增产品
     */
    @ApiOperation("新增产品接口")
    @PostMapping("/add")
    public CommonVoResp<ProductAddVoResp> add(ProductAddVoReq productAddVoReq)
    {
         CommonVoResp<ProductAddVoResp> commonVoResp=new CommonVoResp<>();
         logger.info("新增产品入参：{}",productAddVoReq);
        try {
            ProductAddVoResp productAddVoResp=new ProductAddVoResp();
            String productUuid = tProductService.saveProduct(productAddVoReq);
            productAddVoResp.setProductUuid(productUuid);
            commonVoResp.setData(productAddVoResp);
        } catch (IOException e) {
            commonVoResp = ResponseUtil.setCommonVoRespErrorFile(commonVoResp);
            return commonVoResp;
        }

         logger.info("新增产品出参：{}",commonVoResp);
         return commonVoResp;
    }

    /**
     * 修改产品
     */
    @PostMapping("/udpate")
    @ApiOperation("修改产品接口")
    public CommonVoResp<String> udpate(ProductUpdateVoReq productUpdateVoReq)
    {
        CommonVoResp<String> commonVoResp=new CommonVoResp<>();
        logger.info("修改产品入参：{}",productUpdateVoReq);
        try {
            return ResponseUtil.commonUpdateResult(tProductService.updateProduct(productUpdateVoReq),commonVoResp);
        } catch (IOException e) {
            return ResponseUtil.commonUpdateResult(0,commonVoResp);
        }
    }

    /**
     * 删除产品
     */
    @ApiOperation("删除产品接口-id和productUuid，两者传一即可")
    @PostMapping("/delete")
    public CommonVoResp<String> delete(@RequestBody DeleteProductVoReq deleteProductVoReq)
    {
        CommonVoResp<String> commonVoResp=new CommonVoResp<>();
        if(
                deleteProductVoReq.getProductUuid()==null&&
                        deleteProductVoReq.getId()==null){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        logger.info("删除产品入参：{}",deleteProductVoReq);
        return ResponseUtil.commonDeleteResult(tProductService.deleteTProductById(deleteProductVoReq.getProductUuid(),deleteProductVoReq.getId()),commonVoResp);
    }

    /**
     * 批量删除产品
     */
    @ApiOperation("批量删除产品接口-ids和carouselImageUuids，两者传一即可")
    @PreAuthorize("@ss.hasPermi('ucar:image:remove')")
    @PostMapping("/batchDelete")
    public CommonVoResp<String>  batchDelete(@RequestBody BatchDeleteProductVoReq  batchDeleteProductVoReq)
    {
        CommonVoResp<String> commonVoResp=new CommonVoResp<>();
        if((StringUtils.isNull(batchDeleteProductVoReq.getProductUuids())||batchDeleteProductVoReq.getProductUuids().length==0)
                &&(StringUtils.isNull(batchDeleteProductVoReq.getIds())||batchDeleteProductVoReq.getIds().length==0)){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        logger.info("批量删除产品入参：{}",batchDeleteProductVoReq);
        return ResponseUtil.commonDeleteResult(tProductService.batchDelete(batchDeleteProductVoReq.getProductUuids(),batchDeleteProductVoReq.getIds()),commonVoResp);
    }


}
